package com.cs.paoyer.administration.business.entity;


//import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
//import java.util.HashMap;
//import java.util.Vector;
//import java.sql.PreparedStatement;
//import java.util.Iterator;

import com.cs.paoyer.administration.business.model.FDTypeVO;
//import com.cs.paoyer.administration.business.model.ContractVO;
//import com.cs.paoyer.common.exception.UserException;
//import com.cs.paoyer.common.exception.SystemException;

/**
 * DAO para realizar las consultas de los fondos documentales.
 * 
 * @author Rene.Rangel@consult-soft.com
 * @version 1.0
 */

public class FDTypeDAO
{
  /**
   * Conexion a la base de datos.
   */
  private Connection conn = null;

  /**
   * Constructor que recibe como parametro la conexion con la base de datos.
   * @param conn Conexion con la base de datos.
   */
  
  public FDTypeDAO(Connection conn){
    this.conn = conn;
  }

  
    public Collection findFDTypeByCriterea(FDTypeVO criterea) throws SQLException 
    {
    
      
				String query = "SELECT tifd.* FROM tfd_tipo_fd tifd";

				String filters = "";
				// Filtro por Codigo Interno del Tipo de Fondo Documental
				if ( (criterea.getCode() != null) && !(criterea.getCode().longValue()==0) ) 
				{
						if(filters.equals(""))
						{
								filters = " tifd.codigo=" + criterea.getCode() ;
						}
						else
						{
								filters = " AND tifd.codigo=" + criterea.getCode() ;
						}
				}
				// Filtro por Nombre del Tipo de Fondo Documental
				if ( (criterea.getName() != null) && !(criterea.getName().equals("")) ) 
				{
						if ( filters.equals("") ) 
						{
								filters = filters + " tifd.nombre LIKE '%" + criterea.getName()+"'";
						}
						else
						{
								filters = filters + " AND tifd.nombre LIKE '%" + criterea.getName()+"'";
						}
				}  
				// Filtro por Tipo de Fondo Documental Activo
				if ( (criterea.getActive() != null) && !(criterea.getActive().equals("")) ) 
				{
					if ( filters.equals("") ) 
						{
								filters = filters + " tifd.activo = '"+ criterea.getActive()+"'"; 
						}
						else
						{
								filters = filters + " AND tifd.activo = '"+ criterea.getActive()+"'"; 
						}
				}
				// Filtro por Codigo de la Clase de Fondo Documental Asociada
				if ( (criterea.getFdClassCode() != null) && !(criterea.getFdClassCode().longValue()==0) ) 
				{
						if (filters.equals("")) 
						{
								filters = filters + " tifd.cla_fd_codigo =" + criterea.getFdClassCode();
						}
						else
						{
								filters = filters + " AND tifd.cla_fd_codigo =" + criterea.getFdClassCode();
						}
				}
				
				// Si hay filtros, entonces armar la clausula WHERE 
				if ( !filters.equals("") ) {
					query = query + " WHERE " + filters;
				}

				Statement stm = null;
				ResultSet rst = null;
				ArrayList fdTypes = new ArrayList();
				stm = conn.createStatement();
				rst = stm.executeQuery(query);
				FDTypeVO fdType = null;
				while ( rst.next() ) 
				{
					  fdType = new FDTypeVO();
						fdType.setCode(new Long(rst.getLong("codigo")));
						fdType.setName(rst.getString("nombre"));
						fdType.setActive(rst.getString("activo"));
						fdType.setFdClassCode(new Long(rst.getLong("cla_fd_codigo")));
						fdTypes.add(fdType);
						fdType = null;
			 }
         
			 stm.close();
			 return fdTypes;
  }
  
		  
}